﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="News_Index.aspx.cs" validateRequest="false"  Inherits="ncp.Web.cms.News_Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css" />
    <link href="../../css/base.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../../js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../js/common.js"></script>
	<script src="/Plugin/ueditor/editor_config.js" type="text/javascript"></script>
	<script src="/Plugin/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <link href="/Plugin/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
    <script src="/Plugin/ueditor/editor_all.js" type="text/javascript"></script>
    <link href="/Plugin/ueditor/themes/default/ueditor.css" rel="stylesheet" type="text/css" />
    <script src="/Plugin/uploadify/jquery.uploadify-3.1.min.js" type="text/javascript"></script>
</head>
<body style="margin:0; padding:0px;">
<form id="form1" runat="server">
    <div id="rightlayout" class="easyui-layout"  style="width:100%; height:498px;">
	    <div id="west" region="west" split="true" title="新闻类别" style="width:220px;padding:1px;overflow:auto;">
	        <div class="toolbar" style="background-color:#efefef">
	        </div>
            <div id="divleft"></div>
	    </div>
	    <div id="east" region="center" title="新闻资料">
	        <div class="toolbar" style="background-color:#efefef">
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-add" onclick="onAddNews();">增加</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="onDeleteNews();">删除</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEditNews();">编辑</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-reload" onclick="onSetNewsStatus()">禁用/启用</a>&nbsp;
                <span>查询条件:</span>
                <select id="ddlQuery">
                    <option value="News_Title">文章标题</option>
                    <option value="News_Flag">自定义属性</option>
                    <option value="News_Date">更新时间</option>
                    <option value="News_Times">点击次数</option>
                    <option value="User_Account">发布人</option>
                </select>
                <select name="ddlOperator" id="ddlOperator">
	                <option value="like">包含</option>
	                <option value="=">等于</option>
	                <option value=">">大于</option>
	                <option value="&lt;">小于</option>
                </select>
                <input name="txtQuery" type="text" id="txtQuery" style="width:80px;" />
                <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="onSearch()">查询</a>
	        </div>
            <div id="divright">    
            </div>
	    </div>
	    <div id="divNewsInfoBox">
	        <div class="form mod-content">
            <table>
                <tr>
                    <td class="mod-label"> <label for="newsTitle">文章标题:</label></td>
                    <td>
                    <input type="hidden" id="newsId"/>
                    <input type="text" class="bh-input-long" style="width: 400px" maxlength="100" id="newsTitle"/>
                    <span class="msg"></span>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label"> <label for="newsTitle">所属类别:</label></td>
                    <td id="News_Code">
                        <input id="Checkbox1" type="checkbox" value="002" />农情动态
                        <input id="Checkbox2" type="checkbox" value="003" />科普知识
                        <input id="Checkbox3" type="checkbox" value="004" />政策法规
                        <input id="Checkbox4" type="checkbox" value="005" />农业展会
                        <input id="Checkbox5" type="checkbox" value="006" />市场行情
                        <input id="Checkbox6" type="checkbox" value="007" />技术咨询
                        <input id="Checkbox7" type="checkbox" value="011" />专家指导
                    </td>
                </tr>
                <tr>
                    <td class="mod-label"><label for="newsTitle">来源:</label></td>
                    <td>
                        <input type="text" id="newssource" class="bh-input-def"/>
                        <span>留空则选择自己</span>
                    </td>
                </tr>
                <tr class="meetting">
                    <td class="mod-label"><label for="newsTitle">展会地址:</label></td>
                    <td>
                        <input type="text" id="txtNews_Addr" class="bh-input-def"/>
                    </td>
                </tr>
                <tr class="meetting">
                    <td class="mod-label"><label for="newsTitle">展会时间:</label></td>
                    <td>
                        从&nbsp;<input type="text" class="bh-input-def" id="txtNews_StartDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})"/>&nbsp;到&nbsp;<input type="text" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="bh-input-def" id="txtNews_EndDate"/>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label"> <label for="newsTitle">自定义属性:</label></td>
                    <td align="left" id="flag">
            	        <input type="checkbox" name="flags" id="flagsh" value="h"/>头条[h]
            	        <input  type="checkbox" name="flags" id="flagsc" value="c"/>推荐[c]
            	        <input  type="checkbox" name="flags" id="flagsf" value="f"/>幻灯[f]
            	        <input  type="checkbox" name="flags" id="flagsa" value="a"/>特荐[a]          
            	    </td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"> <label for="proName">缩&nbsp;略&nbsp;图:</label></td>
                    <td>
                        <div id="uploadify1">
                        </div>
                        <span class="msg"></span>
                        <ul id="imageList">
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"><label for="proName">内容摘要:</label></td>
                    <td>
                        <textarea class="description" name="description" cols="3" rows="5" id="description"></textarea>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"><label for="proName">文章内容:</label></td>
                    <td id="news">
                    <script type="text/plain" id="newsContent">
                    </script></td>
                </tr>
                <tr>
            <td colspan="2" style="text-align: center;border-bottom: none;">
                <a href="javascript:void(0)" class="easyui-linkbutton" onclick="onSaveNews()">确定</a>&nbsp;&nbsp;
	            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="onCloseNewsWindow()">关闭</a>
            </td>
        </tr>
            </table>
        </div>
	    </div>
	</div>
	<div class="previewbox">
	    <span title="关闭" onclick="hidePreview()" class="imgdelete"></span>
	    <img  src=""/>
	</div>
	</form>
</body>
</html>
<script type="text/javascript">
    $(function() { //初始化
        LoadNewsClass();
        initDialog();
    });
    
    //加载编辑器
    var editor = new baidu.editor.ui.Editor({
    toolbars: nomalToolbar,
        minFrameHeight: 300,
        elementPathEnabled: false,
        autoHeightEnabled: false
    });
    editor.render("newsContent");
    $(function() {
        //获取cookie用户名
        $.ajax({
            url: "/handler/CookieHandler.ashx",
            type: "POST",
            dataType: "text",
            data: "type=1",
            success: function(data) {
                if (data == "") {
                    alert("获取用户信息失败，请退出重新登录！");
                    return;
                }
                $("#uploadify1").uploadify({
                    'swf': '/Plugin/uploadify/uploadify.swf',
                    'auto': true,
                    'fileTypeExts': '*.gif; *.jpg; *.png',
                    'uploader': '/handler/UploadHandler.ashx',
                    'multi': false,
                    'formData': { 'folder': '/uploadFiles', 'uname': "cms" },
                    'buttonText': '本地图片',
                    "fileSizeLimit": imgSizeLimit,
                    'onUploadSuccess': function(file, d, response) {
                        if (response) {
                            if (d != '0') {
                                appendList(d);
                            } else {
                                alert("图片上传失败！");
                            }
                        }
                    }
                });
            }
        });
    });
    function appendList(fileaddr) {
        //如果存在图片就删除原有图片
        if ($("#imageList li").length > 0) {
            onDeleteImg($(".imgdelete"));
        }
        var list = "<li><span title=\"删除\" onclick=\"onDeleteImg(this)\" class=\"imgdelete\"></span><img  src=\"" + fileaddr + "\"/></li>";
        $("#imageList").html(list);
    }
    //删除图片
    function onDeleteImg(obj) {
        //删除服务器图片
        var src = $(obj).parents("li").children("img").attr("src");
        if (src == undefined || src == "") {
            $(obj).parents("li").remove();
            return;
        }
        $.ajax({
        url: "/handler/MasterImageDelete.ashx",
            type: "POST",
            dataType: "text",
            data: "imgsrc=" + src,
            success: function(data) {
                if (data == "1" || data == "0") {
                    $(obj).parents("li").remove();
                }
            }
        });
    }
    //关闭新闻发布窗口
    function onCloseNewsWindow() {
        emptyData();
        $('#divNewsInfoBox').window('close');
    }
    //清空数据
    function emptyData() {
        $("#newsTitle,#description").val("");
        $("#imageList").html("");
        editor.setContent("");
        $("#flag input").removeAttr("checked");
    }
    
    //初始化对话框
    function initDialog() {
        $('#divNewsInfoBox').dialog({
            title: '网站新闻',
            modal: true,
            width: $(window).width(),
            height: $(window).height(),
            left: 0,
            top: 0,
            padding: 10,
            closed: true,
            draggable: false,
            closable:true
        });
    }
    
    //装载商品类别视图
    function LoadNewsClass() {
        var response = ncp.Web.cms.News_Index.ShowNewsClassTree();
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $("#divleft").html(jsonOBJ.msg);
            } else
                alert(jsonOBJ.msg);
        }
    }

    //关闭商品类别对话框
    function onClose() {
        $('#divNewsClassBox').dialog("close");
    }

    //得到本节点和它的子节点
    function getChildren() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) return "";

        var s = node.id;
        var children = $('#tt1').tree('getChildren', node.target);

        for (var i = 0; i < children.length; i++) {
            s +=","+  children[i].id ;
        }
        return s;
    }

</script>

<%--商品信息--%>
<script type="text/javascript">
    var pageobj = new Object();

    $(function() {
        
        initTree();
        initPageObj();
        initDatagrid();
        LoadNewsInfo();
        initPager();
    });

    //初始化树
    function initTree() {
        $('#tt1').tree({
            onClick: function(node) {
                onSearch();
            }
        });
    }    
    
    //初始化页码对象
    function initPageObj() {
        pageobj.currentpageindex = 1;
        pageobj.pagesize = 10;
        pageobj.sorting = "News_Date";
        pageobj.sortdir = "desc";
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
    }

    //初始化表格
    function initDatagrid() {
        $('#divright').datagrid({
            height: $(window).height() - 61,
            nowrap: true,
            striped: true,
            singleSelect: true,
            sortName: 'News_Date',
            sortOrder: 'desc',
            remoteSort: false,
            idField: 'News_Id',
            pagination: true,
            rownumbers: true,
            columns: [[
					{ field: 'News_Id', title: '文章编号', hidden: 'true', width: 80 },
					{ field: 'News_Title', title: '文章标题', sortable: 'true', width: 360 },
					{ field: 'News_Picture', title: '缩略图', sortable: 'false', width: 40, align: 'center',
					    formatter: function(val, rec) {
					        if (val != "") {
					            return "<img class=\"imgPreview\" onclick=\"imgPreView(this)\" src=\"/cms/images/pic.png\" title=\"预览\" titleimg=\"" + val + "\" style=\"cursor: pointer\">";
					        } else {
					            return "无";
					        }
					    }
					},
					{ field: 'News_Flag', title: '自定义属性', sortable: 'false', width: 100,
					    formatter: function(val, rec) {
					    var str = "<span style=\"color:red;\">";
					        if (val!=null) {
					            if (val.indexOf('h') != -1) {
					                str += "[头条]";
					            }
					            if (val.indexOf('c') != -1) {
					                str += "[推荐]";
					            }
					            if (val.indexOf('f') != -1) {
					                str += "[幻灯]";
					            }
					            if (val.indexOf('a') != -1) {
					                str += "[特荐]";
					            }
					        }
					        str += "</span>";
					        return str;
					    }
					},
					{ field: 'News_Date', title: '更新时间', sortable: 'true', width: 130 },
					{ field: 'NC_Name', title: '文章类别', sortable: 'true', width: 80, align: 'center' },
					{ field: 'News_Times', title: '点击', sortable: 'true', width: 60, align: 'center' },
					{ field: 'User_Account', title: '发布人', sortable: 'true', width: 60, align: 'center' },
					{ field: 'News_Status', title: '状态', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value == '0') {
					            return 'color:red;font-weight:bold;';
					        }
					    },
					    formatter: function(val, rec) {
					        if (val == '0') {
					            return "禁用";
					        } else {
					        return "启用";
					        }
					    }
					}
				]]
        });
    }

    function imgPreView(dom) {
        $(dom).click(function(e) {
            $(".previewbox img").attr("src", $(dom).attr("titleimg"));
            $(".previewbox").css({ "top": e.pageY+10, "left": e.pageX-200 });
            $(".previewbox").fadeIn();
        });
    }
    function hidePreview() {
        $('.previewbox').fadeOut();
    }

    function initPager() {
        var p = $('#divright').datagrid('getPager');
        $(p).pagination({
            total: pageobj.recordcount,
            pageSize: pageobj.pagesize,
            pageList: [10, 15, 20, 25, 30],
            showRefresh: true,
            showPageList:true,
            beforePageText: '第',
            afterPageText: '页 共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录',
            onSelectPage: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onShowPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            }
        });
    }
    
    //翻页
    function onShowPageNo(pageNumber, pageSize) {
        pageobj.pagesize = pageSize;
        pageobj.currentpageindex = pageNumber;
        LoadNewsInfo();
        initPager();
        return false;
    }

    //得到条件
    function GetstrWhere() {
        var myWhere = "Client_Id<1000";
        var nc_code = getChildren();
        if (nc_code != "") myWhere += " and NC_Code in ('" + nc_code + "')";
        var response = ncp.Web.cms.News_Index.GetstrWhere($("#ddlQuery").val(), $("#ddlOperator").val(), $.trim($("#txtQuery").val()));
        if (response.value != null) {
            return myWhere + " and " + response.value;
        }
        else
            return myWhere;
    }

    //得到总记录数
    function GetRecordCount() {
        var response = ncp.Web.cms.News_Index.GetRecordCount(pageobj.currentwhere);
        if (response.value != null) {
            return response.value;
        }
        else
            return 0;
    }

    //查询
    function onSearch() {
        pageobj.currentpageindex = 1;
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
        LoadNewsInfo();
        initPager();
        return false;
    }

    //装载商品信息视图
    function LoadNewsInfo() {
        var jsonStr = $.objectToJson(pageobj);
        var response = ncp.Web.cms.News_Index.BindTable(jsonStr);
        if (response.value != null) {
            var data = eval(response.value);
            $('#divright').datagrid('loadData', data);
        }
    }

    //增加新闻
    function onAddNews() {
        var node = $('#tt1').tree('getSelected');
        $("#newsId").val("");
        if (node == null) {
            alert("请先在左边树里选择一个商品的所属类别！");
            return false;
        }
        $('#divNewsInfoBox').attr("nc_code", node.id);
        $('#divNewsInfoBox').dialog('setTitle', '添加新闻');
        $('#divNewsInfoBox').dialog("open");
        if (node.id == "005")
            $('.meetting').show();
        else
            $('.meetting').hide();
        emptyData();
        return false;
    }
    
    //获取选中的flag
    function getFlag() {
        var flags = "";
        $("#flag input").each(function() {
            if ($(this).attr("checked")) {
                flags +=","+ $(this).val();
            }
        });
        if (flags!="") {
            flags = flags.substring(1);
        }
        return flags;
    }

    function getNews_Code() {
        var News_Code = "";
        $("#News_Code input").each(function() {
            if ($(this).attr("checked")) {
                News_Code += "," + $(this).val();
            }
        });
        if (News_Code != "") {
            News_Code = News_Code.substring(1);
        }
        return News_Code;
    }

    function setNews_Code(News_Code) {
        if (News_Code == null) return;
        if (News_Code.indexOf("002") != -1) {
            $("#Checkbox1").attr("checked", true);
        }
        if (News_Code.indexOf("003") != -1) {
            $("#Checkbox2").attr("checked", true);
        }
        if (News_Code.indexOf("004") != -1) {
            $("#Checkbox3").attr("checked", true);
        }
        if (News_Code.indexOf("005") != -1) {
            $("#Checkbox4").attr("checked", true);
        }
        if (News_Code.indexOf("006") != -1) {
            $("#Checkbox5").attr("checked", true);
        }
        if (News_Code.indexOf("007") != -1) {
            $("#Checkbox6").attr("checked", true);
        }
        if (News_Code.indexOf("011") != -1) {
            $("#Checkbox7").attr("checked", true);
        }
    }
    
    //保存新闻
    function onSaveNews() {
        //执行上传
        var obj = new Object();
        obj.newsId = $("#newsId").val();
        obj.newsTitle = $("#newsTitle").val();
        obj.newsType = $('#divNewsInfoBox').attr("nc_code");
        obj.description = $("#description").val();
        obj.News_Flag = getFlag();
        obj.source = $("#newssource").val();
        obj.newsPic = $("#imageList li img").attr("src") == undefined ? "" : $("#imageList li img").attr("src");
        obj.newsContent = editor.getContent();
        obj.news_addr = $("#txtNews_Addr").val();
        obj.news_startdate = $("#txtNews_StartDate").val();
        obj.news_enddate = $("#txtNews_EndDate").val();
        obj.news_code = getNews_Code();
        var response = ncp.Web.cms.News_Index.OnSave($.objectToJson(obj));
        
        if (response.value != "") {
            var jsonOBJ = eval(response.value)[0];
            alert(jsonOBJ.msg);
            if (jsonOBJ.isreload) {
                onSearch();
                onCloseNewsWindow();
                emptyData();
            }
        }
    }

    //禁用启用文章
    function onSetNewsStatus() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            var newsId = selected.News_Id;
            var status = selected.News_Status == 0 ? 1: 0;
            var response = ncp.Web.cms.News_Index.SetNewsStatus(newsId,status);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                alert(jsonOBJ.msg);
                if (jsonOBJ.isreload) {
                    LoadNewsInfo();
                }
            }
        }
        else
            alert("请先选择一条记录！");
    }

    //编辑文章
    function onEditNews() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divNewsInfoBox').dialog('setTitle', '编辑新闻');
            $('#divNewsInfoBox').dialog("open");
            $("#newsId").val(selected.News_Id);
            $("#newsTitle").val(selected.News_Title);
            $("#newssource").val(selected.News_Source);
            $("#description").val(selected.News_Description);
            $('#divNewsInfoBox').attr("nc_code", selected.NC_Code);
            $("#txtNews_Addr").val(selected.News_Addr);
            $("#txtNews_StartDate").val(selected.News_StartDate);
            $("#txtNews_EndDate").val(selected.News_EndDate);
            setNews_Code(selected.News_Code);
            if (selected.NC_Code == "005")
                $('.meetting').show();
            else
                $('.meetting').hide();
            editor.setContent(selected.News_Note);
            if (selected.News_Picture!="") {
                appendList(selected.News_Picture);
            }
            var flag = selected.News_Flag;
            if (flag!=null) {
                if (flag.indexOf("h") != -1) {
                    $("#flagsh").attr("checked", true);
                }
                if (flag.indexOf("c") != -1) {
                    $("#flagsc").attr("checked", true);
                }
                if (flag.indexOf("f") != -1) {
                    $("#flagsf").attr("checked", true);
                }
                if (flag.indexOf("a") != -1) {
                    $("#flagsa").attr("checked", true);
                }
            }
            
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //删除文章
    function onDeleteNews() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            if (confirm("您确定要删除吗？")) {
                var response = ncp.Web.cms.News_Index.DeleteNewsInfo(selected.News_Id);
                if (response.value != null) {
                    var jsonOBJ = eval(response.value)[0];
                    if (jsonOBJ.isreload) {
                        var index = $('#divright').datagrid('getRowIndex', selected);
                        $('#divright').datagrid('deleteRow', index);
                        $('#divright').datagrid('clearSelections');
                    }
                    alert(jsonOBJ.msg);
                }
            } 
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
</script>
